﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Kinect;

namespace NeuralProto
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class NeuralWindow : UserControl
    {
        private WriteableBitmap bitmap;
        private bool Moving = false;

        byte[] pixels;

        private NeuralNetwork neural = new NeuralNetwork();

        public NeuralWindow()
        {
            InitializeComponent();

            this.DataContext = neural;

            pixels = new byte[neural.averageWidth * neural.averageHeight];

            for (int i = 0; i < pixels.Length; i++)
            {
                pixels[i] = 255;
            }

            bitmap = new WriteableBitmap(neural.averageWidth, neural.averageHeight, 96, 96, PixelFormats.Gray8, BitmapPalettes.Gray256);
        }

        private void drawImage_MouseDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            Moving = true;

            Point p = e.GetPosition(drawImage);

            if (e.LeftButton == MouseButtonState.Pressed)
            {
                draw(false, p);
            }
            else if (e.RightButton == MouseButtonState.Pressed)
            {
                draw(true, p);
            }
        }

        private void drawImage_MouseMove(object sender, System.Windows.Input.MouseEventArgs e)
        {
            if (Moving)
            {
                Point p = e.GetPosition(drawImage);

                if (e.LeftButton == MouseButtonState.Pressed)
                {
                    draw(false, p);
                }
                else if (e.RightButton == MouseButtonState.Pressed)
                {
                    draw(true, p);
                }
            }
        }

        string MatchedHigh = "?", MatchedLow = "?";
        double OutputValueHight = 0, OutputValueLow = 0;

        private void draw(bool white, Point p)
        {
            byte color = white ? (byte)255 : (byte)0;

            int x = (int)(p.X / (100 / neural.averageWidth));
            int y = (int)(p.Y / (150 / neural.averageHeight));

            if (x > 0 && x < neural.averageWidth)
            {
                if (y > 0 && y < neural.averageHeight)
                {
                    pixels[y * neural.averageWidth + x] = color;
                    //pixels[y * neural.averageWidth + x + 1] = color;
                    //pixels[y * neural.averageWidth + x - 1] = color;
                    //pixels[(y - 1) * neural.averageWidth + x] = color;
                    //pixels[(y + 1) * neural.averageWidth + x] = color;
                }
            }

            try
            {
                bitmap.WritePixels(new Int32Rect(0, 0, neural.averageWidth, neural.averageHeight), pixels, neural.averageWidth, 0);
            }
            catch { }

            drawImage.Source = neural.FastResize(bitmap, 100, 150);

            var f = neural.ByteArrayToMatrix(pixels);

            neural.Recognize(f);
        }

        private void drawImage_MouseUp(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            Moving = false;
        }

        private void btnTrain_Click(object sender, RoutedEventArgs e)
        {
            neural.ReTrain();
        }
    }
}
